﻿/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2021/7/10 22:41:46
 *        Description: 暂无
 ***********************************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using InitQ.Abstractions;
using InitQ.Attributes;
using Newtonsoft.Json;

namespace CoreCms.Net.RedisMQ
{
    /// <summary>
    /// 订单退款处理
    /// </summary>
    public class RefundSubscribe : IRedisSubscribe
    {

        private readonly ICoreCmsOrderServices _orderServices;
        private readonly ICoreCmsBillRefundServices _billRefundServices;
        private readonly ISysTaskLogServices _taskLogServices;


        public RefundSubscribe(ICoreCmsOrderServices orderServices, ICoreCmsBillRefundServices billRefundServices, ISysTaskLogServices taskLogServices)
        {
            _orderServices = orderServices;
            _billRefundServices = billRefundServices;
            _taskLogServices = taskLogServices;
        }

        /// <summary>
        /// 订单退款处理
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [Subscribe(RedisMessageQueueKey.RefundSubscribeQueue)]
        private async Task RefundSubscribeQueue(string msg)
        {
            try
            {
                var refundInfo = JsonConvert.DeserializeObject<CoreCmsBillRefund>(msg);

                //去退款
                var toRefundResult = await _billRefundServices.ToRefund(refundInfo.refundId, (int)GlobalEnumVars.BillRefundStatus.STATUS_REFUND);

                //插入退款日志
                var log = new SysTaskLog
                {
                    createTime = DateTime.Now,
                    isSuccess = toRefundResult.status,
                    name = "定时任务取消拼团订单退款日志",
                    parameters = JsonConvert.SerializeObject(toRefundResult)
                };
                await _taskLogServices.InsertAsync(log);

                //更新订单状态为已退款已完成
                await _orderServices.UpdateAsync(p => new CoreCmsOrder()
                {
                    payStatus = (int)GlobalEnumVars.OrderPayStatus.Refunded,
                    status = (int)GlobalEnumVars.OrderStatus.Complete
                }, p => p.orderId == refundInfo.sourceId);

                NLogUtil.WriteAll(NLog.LogLevel.Info, LogType.RedisMessageQueue, "订单退款处理-成功", msg);
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "订单退款处理-异常", msg, ex);
                throw;
            }
            await Task.CompletedTask;
        }


    }
}
